-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Resolved Issue #2939 #2940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
avibega23
wants to merge
1
commit into
TheAlgorithms:master
Choose a base branch
from
avibega23:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Resolved Issue #2939 #2940
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Para de me mandar e-mails seus desgraçados
Em ter., 29 de abr. de 2025, 08:47, Avinoor Singh ***@***.***>
escreveu:
…
1. Input Validation in main() function:
Issue: The original code did not check for non-positive values of n
(array size). As a result, entering a value of n <= 0 would cause buffer
overflow or undefined behavior when the array was accessed.
Change: Added input validation for the array size n to ensure it is
positive. If n <= 0, the program prints an error message and terminates
gracefully:
if (n <= 0) {
std::cerr << "Error: Array size must be a positive integer.\n";
return 1;
}
2. Handling Empty Arrays in median_of_medians() function:
Issue: In the original code, if the array was empty or the median
vector m ended up being empty, the program would try to access m[0],
causing a buffer overflow.
Change: Before accessing m[(sz - 1) / 2], a check was added to ensure that
the median vector m is not empty. If the vector is empty, an error message
is printed, and the program exits:
if (m.empty()) {
std::cerr << "Error: Median vector is empty.\n";
exit(1);
}
3. Graceful Error Handling for Invalid Inputs:
Issue: The code previously did not handle invalid inputs properly, and
it would crash with a segmentation fault when invalid input was given.
Change: Instead of continuing with invalid or empty inputs, the program
now handles such inputs gracefully by printing error messages and
terminating cleanly, thus preventing crashes and undefined behavior.
Example: If n <= 0 is entered, the program prints:
"Error: Array size must be a positive integer."
4. Edge Case Handling in Test Cases:
Issue: The original test cases did not cover edge cases like empty
arrays or non-positive sizes.
Change: Though not explicitly mentioned in the test section, handling of
invalid inputs was prioritized in the main function, ensuring no test cases
would be executed for invalid inputs.
------------------------------
You can view, comment on, or merge this pull request online at:
#2940
Commit Summary
- e8243aa
<e8243aa>
Resolved Issue #2939
File Changes
(1 file <https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940/files>)
- *M* search/median_search.cpp
<https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940/files#diff-b8d10c25804fdac896d6e6f43a12f21332c7b44431237612a6a3f3b107620729>
(17)
Patch Links:
- https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940.patch
- https://github.com/TheAlgorithms/C-Plus-Plus/pull/2940.diff
—
Reply to this email directly, view it on GitHub
<#2940>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BFBT2GMM6IR3UIFPEC4MVWD235RCXAVCNFSM6AAAAAB4CN5OSOVHI2DSMVQWIX3LMV43ASLTON2WKOZTGAZDONJYGE4DQNY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
realstealthninja
requested changes
May 19, 2025
@@ -131,7 +136,11 @@ int main() | |||
int n = 0; | |||
std::cout << "Enter Size of Array: "; | |||
std::cin >> n; | |||
std::vector<int> a(n); | |||
if (n <= 0) { | |||
std::cerr << "Error: Array size must be a positive integer.\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this throw an error instead?
This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue: The original code did not check for non-positive values of n (array size). As a result, entering a value of n <= 0 would cause buffer overflow or undefined behavior when the array was accessed.
Change: Added input validation for the array size n to ensure it is positive. If n <= 0, the program prints an error message and terminates gracefully:
if (n <= 0) {
std::cerr << "Error: Array size must be a positive integer.\n";
return 1;
}
Issue: In the original code, if the array was empty or the median vector m ended up being empty, the program would try to access m[0], causing a buffer overflow.
Change: Before accessing m[(sz - 1) / 2], a check was added to ensure that the median vector m is not empty. If the vector is empty, an error message is printed, and the program exits:
if (m.empty()) {
std::cerr << "Error: Median vector is empty.\n";
exit(1);
}
Issue: The code previously did not handle invalid inputs properly, and it would crash with a segmentation fault when invalid input was given.
Change: Instead of continuing with invalid or empty inputs, the program now handles such inputs gracefully by printing error messages and terminating cleanly, thus preventing crashes and undefined behavior.
Example: If n <= 0 is entered, the program prints:
"Error: Array size must be a positive integer."
Issue: The original test cases did not cover edge cases like empty arrays or non-positive sizes.
Change: Though not explicitly mentioned in the test section, handling of invalid inputs was prioritized in the main function, ensuring no test cases would be executed for invalid inputs.